package com.aauedu.auth.util;

import com.aauedu.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {
    //使用递归方法构建菜单
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
        //创建list集合，用于最终数据
        List<SysMenu> trees = new ArrayList<>();
        //把所有菜单数据进行遍历
        for (SysMenu sysMenu: sysMenuList) {
           //递归入口进入
            // parentId=0是入口
            if (sysMenu.getParentId().longValue() == 0) {
                trees.add(children(sysMenu, sysMenuList));
            }
        }
        return trees;
    }

    /***
     *
     * @param sysMenu  入口
     * @param sysMenuList
     * @return
     */
    private static SysMenu children(SysMenu sysMenu, List<SysMenu> sysMenuList) {
        sysMenu.setChildren(new ArrayList<SysMenu>());
        //遍历所有菜单数据，判断id和parentId对应关系
        for (SysMenu it: sysMenuList) {
            if (sysMenu.getId().longValue() == it.getParentId().longValue()) {
                if (sysMenu.getChildren() == null) {
                    sysMenu.setChildren(new ArrayList<>());
                }
                sysMenu.getChildren().add(children(it, sysMenuList));
            }
        }
        return sysMenu;
    }
}
